package controllers;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Barang;
import models.Kategori;
import system.Controller;
import system.Helper;

/**
 *
 * @author Rezan Achmad
 */
@WebServlet(name = "BarangController", urlPatterns = {"/barang", "/barang/view", "/barang/add", "/barang/update", "/barang/update/image", "/barang/delete", "/barang/admin"})
public class BarangController extends Controller {

    @Override
    protected void initRules() {
        rules.put("/barang/add", identity.isAdmin());
        rules.put("/barang/update", identity.isAdmin());
        rules.put("/barang/update/image", identity.isAdmin());
        rules.put("/barang/delete", identity.isAdmin());
        rules.put("/barang/admin", identity.isAdmin());
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();

        if (userPath.equals("/barang")) {
            userPath = "/barang/index";
        } else if (userPath.equals("/barang/view")) {
            if (isGet) {
                if (request.getParameter("id") != null) {
                    Barang foundItem = Barang.get(Integer.parseInt(request.getParameter("id")));
                    if (foundItem != null) {
                        request.setAttribute("object", foundItem);
                        request.setAttribute("listKategori", Kategori.getAll());
                    } else {
                        userPath = "/barang/index";
                    }
                } else {
                    userPath = "/barang/index";
                }
            } else if (isPost) {
            }
        } else if (userPath.equals("/barang/add")) {
            if (isGet) {
                request.setAttribute("listKategori", Kategori.getAll());
            } else if (isPost) {
                if (!(request.getParameter("kategori").equals("")
                        || request.getParameter("nama_barang").equals("")
                        || request.getParameter("deskripsi").equals("")
                        || request.getParameter("harga_satuan").equals(""))) {
                    Barang newBarang = new Barang();
                    newBarang.kategori_id = Integer.parseInt(request.getParameter("kategori"));
                    newBarang.nama = request.getParameter("nama_barang");
                    newBarang.path_gambar = Barang.IMAGE_DEFAULT;
                    newBarang.deskripsi = request.getParameter("deskripsi");
                    newBarang.harga_satuan = Integer.parseInt(request.getParameter("harga_satuan"));
                    newBarang.poin = Integer.parseInt(request.getParameter("poin"));
                    newBarang.berat = Integer.parseInt(request.getParameter("berat"));
                    newBarang.jumlah = Integer.parseInt(request.getParameter("jumlah"));

                    newBarang.add();
                    userPath = "/barang/index";
                }
            }
        } else if (userPath.equals("/barang/update")) {
            if (isGet) {
                if (request.getParameter("id") != null) {
                    Barang foundItem = Barang.get(Integer.parseInt(request.getParameter("id")));
                    if (foundItem != null) {
                        request.setAttribute("object", foundItem);
                        request.setAttribute("listKategori", Kategori.getAll());
                    } else {
                        userPath = "/barang/index";
                    }
                } else {
                    userPath = "/barang/index";
                }
            } else if (isPost) {
                if (!(request.getParameter("id").equals("")
                        || request.getParameter("kategori").equals("")
                        || request.getParameter("nama_barang").equals("")
                        || request.getParameter("path_gambar").equals("")
                        || request.getParameter("deskripsi").equals("")
                        || request.getParameter("harga_satuan").equals("")
                        || request.getParameter("jumlah").equals(""))) {

                    Barang newBarang = new Barang();
                    newBarang.id = Integer.parseInt(request.getParameter("id"));
                    newBarang.kategori_id = Integer.parseInt(request.getParameter("kategori"));
                    newBarang.nama = request.getParameter("nama_barang");
                    newBarang.path_gambar = request.getParameter("path_gambar");
                    newBarang.deskripsi = request.getParameter("deskripsi");
                    newBarang.harga_satuan = Integer.parseInt(request.getParameter("harga_satuan"));
                    newBarang.jumlah = Integer.parseInt(request.getParameter("jumlah"));
                    newBarang.poin = Integer.parseInt(request.getParameter("poin"));
                    newBarang.berat = Integer.parseInt(request.getParameter("berat"));

                    newBarang.update();
                    userPath = "/barang/index";
                }
            }
        } else if (userPath.equals("/barang/update/image")) {
            if (isGet) {
            } else if (isPost) {
                Barang newBarang = Barang.get(Integer.parseInt(request.getParameter("id")));
                newBarang.path_gambar = Helper.uploadFile(request, "path_gambar");

                newBarang.update();
            }
            userPath = "/barang/index";
        } else if (userPath.equals("/barang/delete")) {
            if (isGet) {
                if (request.getParameter("id") != null) {
                    Barang.delete(Integer.parseInt(request.getParameter("id")));
                }
            } else if (isPost) {
            }
            userPath = "/barang/index";
        }

        if (userPath.equals("/barang/index")) {
            ArrayList<Barang> listBarang = new ArrayList<Barang>();
            ArrayList<String> listNamaKategori = new ArrayList<String>();
            Barang.getAll(listBarang, listNamaKategori);
            request.setAttribute("listBarang", listBarang);
            request.setAttribute("listNamaKategori", listNamaKategori);
        }

        setView(userPath, request, response);
    }
}
